<template>
	<view class="content">
			<view class="un-serchResult-tou">
				<text class="un-serchResult-tou-text">搜索结果</text>
				<text class="iconfont icon-youjiantou" style="font-size: 25upx;"></text>
				<text>{{item}}</text>
			</view>
			<view class="goods-list">
				<!-- 根据上一页面传值cateId判断是否兑换商品 -->

				<!-- 循环渲染商品 -->
				<view 
					v-for="(item, index) in goodsList" :key="index"
					class="goods-item"
					@click="navToDetailPage(item)"
				>
					<view class="image-wrapper">
						<image :src="img+item.THUMB" mode="aspectFill"></image>
					</view>
					<text class="title clamp">{{item.NAME}}</text>
					<view class="price-box">
						<text class="attr clamp">{{item.SHORTINFO}}</text>
						<view class="item1">
							<text class="price" v-if="!item.MULTIPAY">{{item.SALEPRICE}}</text>
							
							<text class="price" v-if="item.MULTIPAY==2&&item.OPPOINT!=0">{{item.OPPOINT}}积分</text>
							<text class="price" v-if="item.MULTIPAY==2&&item.OPDEDU!=0">{{item.OPDEDU}}抵扣券</text>
							<text class="price" v-if="item.MULTIPAY==2&&item.OPCOUPON!=0">{{item.OPCOUPON}}惠券</text>
							
							<text class="price" v-if="item.MULTIPAY==3&&item.MUCHSELLERPRICE!=0">{{item.MUCHSELLERPRICE}}元</text>
							<text class="price" v-if="item.MULTIPAY==3&&item.MUCHSELLERPRICE!=0&&item.MPPOINT!=0">+</text>
							<text class="price" v-if="item.MULTIPAY==3&&item.MPPOINT!=0">{{item.MPPOINT}}积分</text>
							<text class="price" v-if="item.MULTIPAY==3&&(item.MUCHSELLERPRICE!=0||item.MPPOINT!=0)&&item.MPDEDU!=0">+</text>
							<text class="price" v-if="item.MULTIPAY==3&&item.MPDEDU!=0">{{item.MPDEDU}}抵扣券</text>
							<text class="price" v-if="item.MULTIPAY==3&&(item.MUCHSELLERPRICE!=0||item.MPPOINT!=0||item.MPDEDU!=0)&&item.MPCOUPON!=0">+</text>
							<text class="price" v-if="item.MULTIPAY==3&&item.MPCOUPON!=0">{{item.MPCOUPON}}惠券</text>
						</view>
						<text>已售 {{item.SALECOUNT}}</text>
					</view>
				</view>
				
			</view>
			<uni-load-more :status="loadingType"></uni-load-more>
			
			<view class="cate-mask" :class="cateMaskState===0 ? 'none' : cateMaskState===1 ? 'show' : ''" @click="toggleCateMask">
				<view class="cate-content" @click.stop.prevent="stopPrevent" @touchmove.stop.prevent="stopPrevent">
					<scroll-view scroll-y class="cate-list">
						
					</scroll-view>
				</view>
			</view>
		</view>
</template>

<script>
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
	export default {
		components: {
			uniLoadMore	
		},
		data() {
			return {
				cateMaskState: 0, //分类面板展开状态
				headerPosition:"fixed",
				// headerTop:"0px",
				loadingType: 'more', //加载更多状态
				filterIndex: 0, 
				priceOrder: 0, //1 价格从低到高 2价格从高到低
				cateList: [],
				goodsList: [],
				item:'',
				pagesize: 0 ,	//每页条数
				pageNumber: 0 ,  //页码
				totalpage : 0,	//总页数
				totalrow : 0 ,		//总记录数
				img: '',
				ORDERBY: 1,
			};
		},
		onLoad(options) {
			console.log(options.item+'>>>>>>>>>>>>>>>>>')
			this.img = getApp().globalData.imageAddress
			this.item=options.item;
			this.loadData()
		},
		//下拉刷新
		onPullDownRefresh() {
			console.log('refresh');
			this.loadData('refresh');
			setTimeout(function () {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		//加载更多
		onReachBottom(){
			this.loadData();
		},
		methods: {
			//加载商品 ，带下拉刷新和上滑加载
			async loadData(type='add', loading) {
				//没有更多直接返回
				if(type === 'add'){
					if(this.loadingType === 'nomore'){
						uni.showToast({
							title:'没有数据了',
							icon:'none'
						})
						return;
					}
					this.loadingType = 'more';
				}else{
					this.loadingType = 'more'
				}
				
				if(type === 'refresh'){
					this.loadingType = 'more'
					this.pageNumber = ''
				}
								
				this.$http.post("/goods/listAll",{
					SIGNATURE : uni.getStorageSync("SIGNATURE"),
					KEYWORD:this.item,
					PAGENUM: this.loadingType == 'more' ? (this.pageNumber+1) : this.pageNumber
				}).then((res)=>{
					console.log("*******",res)
					let a = res.data.OBJECT
					if(res.data.STATE=='0'&&res.statusCode == 200){
						this.pagesize = a.PAGENUMBER 	//每页条数
						this.pageNumber = a.PAGENUMBER  //页码
						this.totalpage = a.TOTALPAGE	//总页数
						this.totalrow = a.TOTALROW		//总记录数
						let goodsList = a.LIST;
						if(type === 'refresh'){
							this.goodsList = [];
						}
						this.goodsList = this.goodsList.concat(goodsList);
						console.log("000000000",this.goodsList)
					}else{
						uni.showToast({
							title:res.data.MSG,
							icon:'none'
						})
					}
				}).catch((err)=>{
					console.log("request fail",err)
				}).then((e)=>{
					//判断是否还有下一页，有是more  没有是nomore(测试数据判断大于20就没有了)
					this.loadingType  = this.pageNumber == this.totalpage ? 'nomore' : 'more';
					console.log("当前还有吗？",this.loadingType)
					if(type === 'refresh'){
						if(loading == 1){
							uni.hideLoading()
						}else{
							uni.stopPullDownRefresh();
						}
					}
				})
			},
			//普通商品详情
			//根据商品的联查结果是否有某个参数
			//商品详情页
			navToDetailPage(item) {
				if(item.MULTIPAY){
					let ids=item.ID;
					let sid=item.SID;
					uni.navigateTo({
						url: `/pages/product/productExchange?id=${ids}&sid=${sid}`
					})
				}else{
					let id = item.ID;
					uni.navigateTo({
						url: `/pages/product/product?id=${id}`
					})
				}
			},
		},
	}
</script>

<style lang="scss">
	page, .content{
		background: $page-color-base;
	}
	// .content{
	// 	padding-top: 96upx;
	// }
	.un-serchResult-tou{
		margin:30upx 30upx;
		.un-serchResult-tou-text{
			font-size: 28upx;
		}
	}
	
	.navbar{
		position: fixed;
		left: 0;
		top: var(--window-top);
		display: flex;
		width: 100%;
		height: 80upx;
		background: #fff;
		box-shadow: 0 2upx 10upx rgba(0,0,0,.06);
		z-index: 10;
		.nav-item{
			flex: 1;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100%;
			font-size: 30upx;
			color: $font-color-dark;
			position: relative;
			&.current{
				color: $base-color;
				&:after{
					content: '';
					position: absolute;
					left: 50%;
					bottom: 0;
					transform: translateX(-50%);
					width: 120upx;
					height: 0;
					border-bottom: 4upx solid $base-color;
				}
			}
		}
		.p-box{
			display: flex;
			flex-direction: column;
			.yticon{
				display: flex;
				align-items: center;
				justify-content: center;
				width: 30upx;
				height: 14upx;
				line-height: 1;
				margin-left: 4upx;
				font-size: 26upx;
				color: #888;
				&.active{
					color: $base-color;
				}
			}
			.xia{
				transform: scaleY(-1);
			}
		}
		.cate-item{
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100%;
			width: 80upx;
			position: relative;
			font-size: 44upx;
			&:after{
				content: '';
				position: absolute;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
				border-left: 1px solid #ddd;
				width: 0;
				height: 36upx;
			}
		}
	}

	/* 分类 */
	.cate-mask{
		position: fixed;
		left: 0;
		top: var(--window-top);
		bottom: 0;
		width: 100%;
		background: rgba(0,0,0,0);
		z-index: 95;
		transition: .3s;
		
		.cate-content{
			width: 630upx;
			height: 100%;
			background: #fff;
			float:right;
			transform: translateX(100%);
			transition: .3s;
		}
		&.none{
			display: none;
		}
		&.show{
			background: rgba(0,0,0,.4);
			
			.cate-content{
				transform: translateX(0);
			}
		}
	}
	.cate-list{
		display: flex;
		flex-direction: column;
		height: 100%;
		.cate-item{
			display: flex;
			align-items: center;
			height: 90upx;
			padding-left: 30upx;
 			font-size: 28upx;
			color: #555;
			position: relative;
		}
		.two{
			height: 64upx;
			color: #303133;
			font-size: 30upx;
			background: #f8f8f8;
		}
		.active{
			color: $base-color;
		}
	}

	/* 商品列表 */
	.goods-list{
		display:flex;
		flex-wrap:wrap;
		padding: 0 30upx;
		background: #fff;
		.goods-item{
			display:flex;
			flex-direction: column;
			width: 48%;
			padding-bottom: 40upx;
			&:nth-child(2n+1){
				margin-right: 4%;
			}
		}
		.image-wrapper{
			width: 100%;
			height: 330upx;
			border-radius: 3px;
			overflow: hidden;
			image{
				width: 100%;
				height: 100%;
				opacity: 1;
			}
		}
		.title{
			font-size: $font-lg;
			color: $font-color-dark;
			line-height: 80upx;
		}
		.price-box{
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding-right: 10upx;
			font-size: 24upx;
			color: $font-color-light;
		}
		.price{
			font-size: $font-lg;
			color: $uni-color-primary;
			line-height: 1;
			// &:before{
			// 	content: '￥';
			// 	font-size: 26upx;
			// }
		}
		.un-huangtuoyuan{
					z-index: 1;
					position: absolute;
					margin-left: 180upx;
					margin-bottom: 200upx;
					top: -18upx;
					right: 20upx;
					width: 79upx;
					height: 35upx;
					background: inherit;
					background-color: rgba(241, 151, 54, 1);
					box-sizing: border-box;
					border-width: 1upx;
					border-style: solid;
					border-color: rgba(241, 151, 54, 1);
					border-radius: 42upx;
					border-bottom-left-radius: 0upx;
					-moz-box-shadow: none;
					-webkit-box-shadow: none;
					box-shadow: none;
					font-family: '微软雅黑';
					font-weight: 404;
					font-style: normal;
					font-size: 22upx;
					color: #FFFFFF;
					text-align: center;
					line-height: 31upx
				}
				.un-renimage-wrapper{
					margin-left: 95upx;
					width: 160upx;
					height: 160upx;
					border-radius: 3px;
					overflow: hidden;
					image{
						width: 100%;
						height: 100%;
						opacity: 1;
					}
				}
	}
	

</style>
